package com.sharekitchen.controller;


import com.sharekitchen.common.R;
import com.sharekitchen.entity.Admin;
import com.sharekitchen.service.AdminService;
import com.sharekitchen.utils.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/register")
    public R<String> register(@RequestBody Admin admin) {
//        检验用户名和密码为空
        if (util.isEmptyOrNull(admin.getAdminName()) || util.isEmptyOrNull(admin.getPassword())) {
            return R.fail("用户名或密码不可为空,注册失败");
        }
//            不存在该用户
        if (adminService.existAdminName(admin.getAdminName())) {
            return R.fail("用户名重复,注册失败");
        }
//             通过校验
        if (adminService.addAdmin(admin)) {
            return R.ok("注册成功~", null);
        }
        return R.error("注册失败~");
    }

    @PostMapping("/login")
    public R<String> login(@RequestBody Admin admin) {
        String adminName = admin.getAdminName();
        String password = admin.getPassword();
//        用户名密码不为空
        if (util.isEmptyOrNull(adminName) || util.isEmptyOrNull(password)) {
            return R.fail("用户名或密码不可为空,登录失败");
        }
        //            不存在该用户
        if (!(adminService.existAdminName(adminName))) {
            return R.fail("用户不存在,登录失败");
        }
        //        通过校验
        if (adminService.findAdminByName(adminName).getPassword().equals(password)) {
            return R.ok("登陆成功~", null);
        }
        return R.error("密码错误,登录失败");

    }

    @GetMapping("/name")
    public R<Admin> getAdminByName(@RequestParam String adminName) {
        if (util.isEmptyOrNull(adminName)) {
            return R.fail("用户名不能为空~,查询失败");
        }
        if (adminService.existAdminName(adminName)) {
            return R.fail("用户不存在");
        }
        Admin admin = adminService.findAdminByName(adminName);
        if (admin != null) {
            System.out.println(admin);
            return R.ok("查询成功~", admin);
        }
        return R.error("查询失败");
    }

    @GetMapping("/all")
    public R<List<Admin>> getAllAdmin() {
        List<Admin> allAdmin = adminService.findAllAdmin();
        if (allAdmin != null) {
            log.info(allAdmin.toString());
            return R.ok("查询成功", allAdmin);
        }
        return R.error("查询失败");
    }

    @DeleteMapping("/delete")
    public R<String> deleteAdminByName(@RequestParam String adminName) {
        if (util.isEmptyOrNull(adminName)) {
            return R.fail("用户名不能为空,删除失败");
        }
        if (!adminService.existAdminName(adminName)) {
            return R.fail("该用户不存在,删除失败~");
        }
        if (adminService.deleteAdminByName(adminName)) {
            return R.ok("删除成功", null);
        }
        return R.error("删除失败");
    }

    @PutMapping("/change")
    public R<String> changeAdmin(@RequestBody Admin admin) {
        if (adminService.updateAdmin(admin)) {
            return R.ok("修改成功", null);
        }
        return R.error("修改失败");
    }

}
